package com.ruoyi.business.controller.app;


import com.ruoyi.business.service.IWsdMealsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.vo.MealInfoVO;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 餐饮时间表 前端控制器  小程序
 * </p>
 *
 * @author wwq
 * @since 2024-04-20
 */
@RestController
@RequestMapping("/wx_meals")
@Slf4j
public class WxAppWsdMealsController extends BaseController {


    @Autowired
    private IWsdMealsService wsdMealsService;

    /**
     * 查询当天学校食谱
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam(required = false) String specificDate) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        List<MealInfoVO> mealInfoVO = wsdMealsService.selectMealInfo(specificDate,schoolId);
        return AjaxResult.success(mealInfoVO);
    }

    /**
     * 查询范围的时间
     */
    @GetMapping("/time_scope")
    public AjaxResult timeScope(@RequestParam(required = false) String specificDate) {
        LocalDate today = null;
        if (StringUtils.isEmpty(specificDate)){
            // 获取当前日期
            today = LocalDate.now();
        }else {
            today = LocalDate.parse(specificDate);
        }

        // 定义日期格式化器，设置英文星期显示
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd EEE", Locale.ENGLISH);

        // 获取前一天的日期
        List<String> dateList = new ArrayList<>();
        LocalDate previousDay = today.minusDays(1);
        dateList.add(previousDay.format(formatter));

        // 获取今天的日期并加入列表
        dateList.add(today.format(formatter));

        // 输出接下来3天的日期
        for (int i = 1; i <= 3; i++) {
            LocalDate nextDay = today.plusDays(i);
            dateList.add(nextDay.format(formatter));
        }
        log.info(dateList.toString());
        return AjaxResult.success(dateList);
    }
}
